package com.zyf.project.capchain.blockchain.service;

import com.zyf.project.capchain.blockchain.utils.BlockCache;
import com.zyf.project.capchain.blockchain.websocket.P2pClient;
import com.zyf.project.capchain.blockchain.websocket.P2pServer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

/**
 * @author Zhou Yifan
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class P2pRunService implements ApplicationRunner {

    private final BlockCache blockCache;

    private final P2pClient p2pClient;

    private final P2pServer p2pServer;

    @Override
    public void run(ApplicationArguments args) {
        p2pServer.initP2PServer(blockCache.getP2pport());
        p2pClient.connectToPeer(blockCache.getAddress());
        log.info("*****难度系数******" + blockCache.getDifficulty());
        log.info("*****端口号******" + blockCache.getP2pport());
        log.info("*****节点地址******" + blockCache.getAddress());
    }
}
